#include "StdAfx.h"
#include "Settings.h"
#include "Addin.h"
#include <fstream>
#include <iostream>

Settings::Settings(void)
{
}

Settings::~Settings(void)
{
}

void Settings::load()
{
	tstring config = CAddin::DLLPATH;
	config.append(_T("encryptit.config"));
	std::wfstream file(config.c_str());
	/*if(!file.is_open())
	{
		std::ofstream ofile(config.c_str());
		ofile.close();
		file.open(config.c_str());
	}*/
	int parsedParams = 0;
	tstring line;
	if(file.is_open())
	{
		while(std::getline(file,line) && parsedParams < 6) 
		{			
			  parsedParams++;
			  switch(parsedParams)
			  {
				  case 1:
					  enabled = enabledOnStart = (line!=_T("0"));
					  break;
				  case 2:
					  alwayson = alwaysonOnStart = (line!=_T("0"));
					  break;
				  case 3:
					  useregex = useregexOnStart = (line!=_T("0"));
					  break;
				  case 4:
					  checksubj = checksubjOnStart = (line!=_T("0"));
					  break;
				  case 5:			
					mask = maskOnStart = line;
					  break;
				  default:
					  break;
			  }
		}
		file.close();
	}
}

void Settings::save()
{
	tstring config = CAddin::DLLPATH;
	config.append(_T("encryptit.config"));
	//std::ofstream file(config.c_str(), std::ios::trunc | std::ios::out | std::ios::binary);
	std::wofstream file(config.c_str(), std::ios::trunc);
	if(file.is_open())
	{
		//wchar_t BOM = 0xFEFF;		
		//file.write((char *) &BOM,sizeof(wchar_t));	
		//file << BOM;

		char buf[2];
		_itoa(enabled, buf, 2);
		file << buf << std::endl;
		//file.write((char *) buf, 2 * sizeof(char));
		//buf[0]='\n';
		//buf[1]=0;
		//file.write((char *) buf, 2 * sizeof(char));

		_itoa(alwayson, buf, 2);
		file << buf << std::endl;
		//file.write((char *) buf, 2 * sizeof(char));
		//buf[0]='\n';
		//buf[1]=0;
		//file.write((char *) buf, 2 * sizeof(char));

		_itoa(useregex, buf, 2);
		file << buf << std::endl;
		//file.write((char *) buf, 2 * sizeof(char));
		//buf[0]='\n';
		//buf[1]=0;
		//file.write((char *) buf, 2 * sizeof(char));

		_itoa(checksubj, buf, 2);
		file << buf << std::endl;
		//file.write((char *) buf, 2 * sizeof(char));
		//buf[0]='\n';
		//buf[1]=0;
		//file.write((char *) buf, 2 * sizeof(char));

		file << mask.c_str() << std::endl;		
		//file.write((char *) mask.c_str(), mask.length() * sizeof(wchar_t));
		//buf[0]='\n';
		//buf[1]=0;
		//file.write((char *) buf, 2 * sizeof(char));

		file.flush();
		file.close();
	}
}

void Settings::getSettings(VARIANT_BOOL &enbld, VARIANT_BOOL &alwon, VARIANT_BOOL &usergx, VARIANT_BOOL &chksubj, tstring &msk)
{
	enbld = enabled;
	alwon = alwayson;
	usergx = useregex;
	chksubj = checksubj;
	msk =  mask;
}